(function () {
    var RockerUI = (function () {
        function RockerUI() {
            RockerUI.super(this);

            this._panel     = null;
            this._arrow     = null;
            this._stickBG   = null;
            this._stickWait = null;
            this._stickMove = null;

            this.centerPointX = 0;
            this.centerPointY = 0;
            this.MAX_DISTANCE = 0;

            this._init();
        }
        Laya.class(RockerUI, "", Laya.Box);
        var _proto = RockerUI.prototype;

        _proto._init = function () {
            var panel       = new Laya.Image("res/game/rocker/panel.png");
            var arrow       = new Laya.Image("res/game/rocker/arrow.png");
            var stickBG     = new Laya.Image("res/game/rocker/stick_bg.png");
            var stickWait   = new Laya.Image("res/game/rocker/stick_wait.png");
            var stickMove   = new Laya.Image("res/game/rocker/stick_moving.png");

            this._panel     = panel;
            this._arrow     = arrow;
            this._stickBG   = stickBG;
            this._stickWait = stickWait;
            this._stickMove = stickMove;

            this.width  = panel.width;
            this.height = panel.height;

            this.centerPointX = panel.width / 2;
            this.centerPointY = panel.height / 2;
            this.MAX_DISTANCE = this.centerPointX - stickMove.width / 2;

            panel.alpha = 0.3;

            arrow.pivot(arrow.width / 2, panel.height / 2 + arrow.height / 2);
            arrow.visible = false;
            arrow.pos(this.centerPointX, this.centerPointY);

            stickBG.anchorX = 0.5;
            stickBG.anchorY = 0.5;
            stickBG.alpha   = 0.5;
            stickBG.pos(this.centerPointX, this.centerPointY);

            stickWait.anchorX = 0.5;
            stickWait.anchorY = 0.5;
            stickWait.alpha   = 0.7;
            stickWait.pos(this.centerPointX, this.centerPointY);

            stickMove.anchorX = 0.5;
            stickMove.anchorY = 0.5;
            stickMove.visible = false;
            stickMove.pos(this.centerPointX, this.centerPointY);

            this.addChildren(panel, arrow, stickBG, stickWait, stickMove);
        }
        
        _proto.move = function (data) {
            this._arrow.visible = true;
            this._arrow.rotation = data.angle;
            this._stickMove.x = data.position.x + this.centerPointX;
            this._stickMove.y = data.position.y + this.centerPointY;
            this._stickMove.visible = true;

            this._stickBG.alpha = 0.6;
            this._stickWait.visible = false;
            this._panel.alpha = 0.4;
        }
        _proto.reset = function () {
            this._stickBG.alpha = 0.5;
            this._stickMove.visible = false;
            this._stickWait.visible = true;
            this._panel.alpha = 0.3;
            this._arrow.visible = false;
        }

        return RockerUI;
    })();

    (function (_super) {
        function Rocker(type, config) {
            Rocker.super(this);
            this._rocker = null;
            
            this._isMoving = false;
            this._centerPointX = 0;
            this._centerPointY = 0;

            this._direction = null;
            this._angle = null;
            this._distance = 0;
            this._MAX_DISTANCE = 0;

            this._touchId = null;

            this.config = null;

            this._init(config);
        }
        Laya.class(Rocker, "Game.Rocker", _super);
        var _proto = Rocker.prototype;
        _proto.destroy = function () {
            console.log("destroy rocker");
            _super.prototype.destroy.call(this, true);

            this._rocker = null;
            this._isMoving = null;
            this._centerPointX = null;
            this._centerPointY = null;
            this._direction = null;
            this._angle = null;
            this._distance = null;
            this._MAX_DISTANCE = null;
            this._touchId = null;
            this.config = null;

            this.off(Laya.Event.MOUSE_DOWN, this, this._onMouseDown);

            Laya.stage.off(Laya.Event.KEY_DOWN, this, this._onKeyDown);
            Laya.stage.off(Laya.Event.KEY_UP, this, this._onMouseUp);
            
            Laya.stage.off(Laya.Event.MOUSE_MOVE, this, this._onMouseMove);
            Laya.stage.off(Laya.Event.MOUSE_UP, this, this._onMouseUp);
            Laya.stage.off(Laya.Event.MOUSE_OUT, this, this._onMouseUp);
        }
        
        _proto._init = function (config) {
            this.config = config;
            this._create();
            this._bindEvent();

            $.extend(this, config, true);
        }
        _proto._create = function () {
            var rocker = new RockerUI();
            
            this.width          = rocker.width;
            this.height         = rocker.height;
            this._MAX_DISTANCE  = rocker.MAX_DISTANCE;
            this._centerPointX  = rocker.centerPointX;
            this._centerPointY  = rocker.centerPointY;
            this._rocker        = rocker;

            this.addChild(rocker);
        }
        _proto._bindEvent = function () {
            this.on(Laya.Event.MOUSE_DOWN, this, this._onMouseDown);

            Laya.stage.on(Laya.Event.KEY_DOWN, this, this._onKeyDown);
            Laya.stage.on(Laya.Event.KEY_UP, this, this._onMouseUp);
            
            Laya.stage.on(Laya.Event.MOUSE_MOVE, this, this._onMouseMove);
            Laya.stage.on(Laya.Event.MOUSE_UP, this, this._onMouseUp);
            Laya.stage.on(Laya.Event.MOUSE_OUT, this, this._onMouseUp);
        }

        _proto._onKeyDown = function (event) {
            switch (event.keyCode) {
                case 37:
                    this._isMoving = true;
                    this._update({x : this._centerPointX - this._MAX_DISTANCE, y : this._centerPointY});
                    break;
                case 38:
                    this._isMoving = true;
                    this._update({x : this._centerPointX, y : this._centerPointY - this._MAX_DISTANCE});
                    break;
                case 39:
                    this._isMoving = true;
                    this._update({x : this._centerPointX + this._MAX_DISTANCE, y : this._centerPointY});
                    break;
                case 40:
                    this._isMoving = true;
                    this._update({x : this._centerPointX, y : this._centerPointY + this._MAX_DISTANCE});
                    break;
            }
        }
        _proto._onMouseDown = function (e) {
            console.log("Rocker MouseDown:e.touchId-" + e.touchId + "this._touchId-" + this._touchId, (this._touchId != null && this._touchId != e.touchId));
            if(this._touchId != null && this._touchId != e.touchId){
                return;
            }
            this._touchId = e.touchId;
            this._isMoving = true;
            this._update(this.getMousePoint());
        }
        _proto._onMouseUp = function (e) {
            console.log("Rocker MouseUp:e.touchId-" + e.touchId + "this._touchId-" + this._touchId, ((e.touchId != (void 0) ||  e.touchId != null) && this._touchId != e.touchId));
            if((e.touchId != (void 0) ||  e.touchId != null) && this._touchId != e.touchId){
                return;
            }
            if(e.keyCode && (e.keyCode < 37 || e.keyCode > 40)){
                return;
            }
            this._update({x : this._centerPointX, y : this._centerPointY});
            this._isMoving = false;
            this._touchId = null;
        }
        _proto._onMouseMove = function (e) {
            if((e.touchId != (void 0) ||  e.touchId != null) && this._touchId != e.touchId){
                return;
            }
            if(this._isMoving){
                this._update(this.getMousePoint());
            }
        }

        _proto._update = function (point) {
            if(!this._isMoving){return;}

            var data = null;
            var mouseX = point.x - this._centerPointX;
            var mouseY = point.y - this._centerPointY;
            var p = new Laya.Point(mouseX, mouseY);
            var distance = p.distance(0, 0);
            this._distance = distance;
            this._angle = 180 * Math.atan2(mouseY, mouseX) / Math.PI + 90;

            if(mouseX === 0 && mouseY === 0){
                this._rocker.reset();
                this.event("reset");

                return;
            }

            if(this._angle > -45 && this._angle <= 45){
                this._direction = "up";
            }else if(this._angle > 45 && this._angle <= 135){
                this._direction = "right";
            }else if(this._angle > 135 && this._angle <= 225){
                this._direction = "down";
            }else{
                this._direction = "left";
            }

            if(distance > this._MAX_DISTANCE){
                distance = this._MAX_DISTANCE / distance;
                this._distance = this._MAX_DISTANCE;
                mouseX *= distance;
                mouseY *= distance;
            }

            this._distance = this._distance / this._MAX_DISTANCE;

            data = {angle : this._angle, distance : this._distance, direction : this._direction, position : {x : mouseX, y : mouseY}};
            this._rocker.move(data);
            this.event("moving", data);
        }

        _proto.resize = function (height) {
            $.extend(this, this.config, true);
        }
    })(Laya.Box);
})();